package com.digitalchina.lxnsy.zbsy;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.toolkit.CustomDialog;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.OkHttpUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.xml.RongCloudXML;
import com.digitalchina.lxnsy.xml.UserXML;
import com.lzy.okhttputils.model.HttpParams;
import com.ta.utdid2.android.utils.StringUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TagManageAddActivity extends BaseActivity {
    protected final int REQUEST_CODE_1 = 111;


    @Bind(R.id.et_earLabelNo)
    EditText etEarLabelNo;
    @Bind(R.id.tv_chipNo)
    TextView tvChipNo;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_tag_manage_add);
        setTitle("新增耳标信息");
    }

    @Override
    public void initMember() {

    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                switch (message.what){
                    case MSG_S_0:
                        setResult(RESULT_OK);
                        CustomDialog.nativeDialogFinish(activity, "保存成功");
                        break;
                    case MSG_F_0:
                        showToast(message.obj.toString());
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.bt_scan, R.id.bt_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_scan:
                GotoUtil.gotoForResultActivity(activity, TagManageScanActivity.class, REQUEST_CODE_1);
                break;
            case R.id.bt_save:
                save();
                break;
        }
    }

    private void save() {
        if(StringUtil.isEditEmpty(etEarLabelNo)){
            showToast("请输入耳标号");
            return;
        }



        if(StringUtil.isTextEmpty(tvChipNo)){
            showToast("请输入芯片号");
            return;
        }


        try {
            final JSONObject params = new JSONObject();

            params.put("earLabelNo", etEarLabelNo.getText().toString());
                    params.put("chipNo", tvChipNo.getText().toString());
          //  params.put("chipNo", "E2806894000040093CB26103");
            params.put("subjectInfoId", RongCloudXML.getSubjectInfoId(activity));
            params.put("tenantCode", StringUtils.isEmpty(RongCloudXML.getTenantCode(activity))?"T000010":RongCloudXML.getTenantCode(activity));

            OkHttpUtil.post(activity, URL.earLabelInfo_save, "正在保存", params, mHandler,
                    MSG_S_0, MSG_F_0);

        } catch (JSONException e) {
            e.printStackTrace();
        }









    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            String chipNo = (String) data.getSerializableExtra("chipNo");
            if(!StringUtil.isStrEmpty(chipNo)){
                tvChipNo.setText(chipNo);
            }
        }
    }
}
